package com.gardenguell.salad.ui.adapter;

import android.media.Image;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.gardenguell.salad.R;
import com.gardenguell.salad.data.model.Box;
import com.gardenguell.salad.data.model.Vegetable;

import java.util.Collections;
import java.util.List;

/**
 * Created by absurd on 16/2/12.
 */
public class VegetableAdapter extends ClickableRecyclerViewAdapter<VegetableAdapter.ItemViewHolder> {



    private List<Vegetable> mData = Collections.emptyList();
    private boolean canAdd = false;

    public VegetableAdapter(List<Vegetable> data, boolean canAdd){
        if(data != null) {
            this.mData = data;
        }
        this.canAdd = canAdd;
    }


    public void setData(List<Vegetable> data){
        this.mData = data;
        notifyDataSetChanged();
    }


    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_vegetable, parent, false);
        view.setOnClickListener(this);
        view.findViewById(R.id.btn_add).setOnClickListener(this);
        return new ItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        Vegetable vegetable = mData.get(position);
        holder.addBtn.setTag(vegetable);
        holder.vegetableImage.setImageResource(vegetable.getImage());
        holder.vegetableName.setText(vegetable.getName());
        if(canAdd){
            holder.addBtn.setVisibility(View.VISIBLE);
        }else{
            holder.addBtn.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public static class ItemViewHolder extends RecyclerView.ViewHolder{
        public ImageView vegetableImage;
        public TextView vegetableName;
        public TextView vegetableDesc;
        public ImageView addBtn;

        public ItemViewHolder(View itemView) {
            super(itemView);
            vegetableImage = (ImageView)itemView.findViewById(R.id.vegetable_image);
            vegetableName = (TextView)itemView.findViewById(R.id.vegetable_name);
            vegetableDesc = (TextView)itemView.findViewById(R.id.vegetable_desc);
            addBtn = (ImageView)itemView.findViewById(R.id.btn_add);
        }
    }






}
